package de.carcophan.jimdb.gwt.client.presenter;

import java.util.Arrays;
import java.util.List;

import net.auroris.ColorPicker.client.ColorPicker;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import de.carcophan.jimdb.gwt.client.ClientFactory;
import de.carcophan.jimdb.gwt.client.service.MovieServiceAsync;
import de.carcophan.jimdb.gwt.client.view.MovieGridViewImpl;
import de.carcophan.jimdb.gwt.shared.entites.Tag;
import de.carcophan.jimdb.gwt.shared.entites.UserMovies;

public class SearchPresenterImpl implements SearchPresenter {

	private MovieServiceAsync movieSerivce;
	private Display display;
	private ClientFactory clientFactory;

	public SearchPresenterImpl(ClientFactory clientFactory, Display display) {
		this.clientFactory = clientFactory;
		this.movieSerivce = clientFactory.getMovieService();
		this.display = display;
		display.setPresenter(this);
		init();
	}

	private void init() {

		movieSerivce.findAllTags(new AsyncCallback<List<Tag>>() {

			@Override
			public void onFailure(Throwable t) {
				Window.alert(Arrays.toString(t.getStackTrace()));
			}

			@Override
			public void onSuccess(List<Tag> tags) {
				setupTagsForDisplay(tags);
				display.setTags(tags);
			}

		});
	}

	private void setupTagsForDisplay(List<Tag> tags) {
		Tag emptyTag = new Tag();
		emptyTag.setName("");
		tags.add(0, emptyTag);
	}

	public interface Display extends HasText {
		Widget asWidget();

		void setDeleteTagImage(ImageResource deleteTagImage);

		void setPresenter(SearchPresenter presenter);

		CellList<UserMovies> getCellList();

		UserMovies getSelectedMovie();

		void setTags(List<Tag> tags);

		String getSelectedTag();

		String getTextBoxValue();

		void setTagForSelectedMovie(Tag tag);

		void refreshScrollPanel();

		String getSearchValue();

		MovieGridViewImpl getMovieGrid();
	}

	@Override
	public void go(HasWidgets panel) {
		panel.add(display.asWidget());
	}

	public void onSubmit() {
		clientFactory.getWidgetUtil().showAjaxLoader(true);
		movieSerivce.findMovieBySearchValue(display.getSearchValue(),
				new AsyncCallback<List<UserMovies>>() {
					@Override
					public void onSuccess(List<UserMovies> userMovies) {
						if (userMovies != null) {
							display.getCellList().setRowData(userMovies);
							display.refreshScrollPanel();
						}
						clientFactory.getWidgetUtil().showAjaxLoader(false);
					}

					@Override
					public void onFailure(Throwable t) {
						clientFactory.getWidgetUtil().showAjaxLoader(false);
						Window.alert(t.getMessage());
					}
				});
	}

	@Override
	public void onSelect() {
		movieSerivce.findTagForMovie(display.getSelectedMovie().getMovie()
				.getImdbId(), new AsyncCallback<Tag>() {

			@Override
			public void onFailure(Throwable t) {
				Window.alert(Arrays.toString(t.getStackTrace()));
			}

			@Override
			public void onSuccess(Tag tag) {
				display.setTagForSelectedMovie(tag);
			}
		});
	}

	@Override
	public void onSubmitButtonClicked() {
		final String tag = display.getTextBoxValue();

		if (tag != null && !tag.trim().isEmpty()) {

			final ColorPicker picker = new ColorPicker();
			final DialogBox dialogBox = new DialogBox();

			VerticalPanel panel = new VerticalPanel();
			panel.setSize("450px", "300px");
			Label label = new Label("Choose Color for Tag");
			HorizontalPanel headingPanel = new HorizontalPanel();
			headingPanel.setSpacing(5);
			headingPanel.add(label);
			panel.add(headingPanel);

			panel.add(picker);
			HorizontalPanel horizontalPanel = new HorizontalPanel();
			panel.add(horizontalPanel);
			Button pick = new Button("Pick");
			pick.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent arg0) {
					String hexColor = picker.getHexColor();
					dialogBox.hide();

					movieSerivce.addTag(tag, hexColor,
							new AsyncCallback<List<Tag>>() {
								@Override
								public void onSuccess(List<Tag> tags) {
									setupTagsForDisplay(tags);
									display.setTags(tags);
								}

								@Override
								public void onFailure(Throwable t) {
									Window.alert(Arrays.toString(t
											.getStackTrace()));
								}
							});

				}
			});
			horizontalPanel.add(pick);
			Button cancel = new Button("Cancel");
			cancel.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent arg0) {
					dialogBox.hide();
				}
			});
			horizontalPanel.add(cancel);
			dialogBox.add(panel);
			dialogBox.setAnimationEnabled(true);
			dialogBox.show();
			dialogBox.center();
		}
	}

	@Override
	public void tagSelected() {
		String selectedTag = display.getSelectedTag();
		UserMovies selectedMovie = display.getSelectedMovie();
		if (selectedMovie != null) {
			movieSerivce.saveTag(selectedMovie.getMovie().getImdbId(),
					selectedTag, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable t) {
							Window.alert(Arrays.toString(t.getStackTrace()));
						}

						@Override
						public void onSuccess(Void v) {
							onSubmit();
						}
					});
		}
	}

	public interface TagWidget {
		Widget asWidget();

		void setDeleteTagImage(ImageResource deleteTagImage);

		void setTags(List<Tag> tags);

		void setPresenter(SearchPresenter presenter);

		String getTextBoxValue();

		String getSelectedTag();
	}

	@Override
	public void tabSelected(Widget tabWidget) {
		Presenter presenter = new MovieGridPresenterImpl(clientFactory,
				display.getMovieGrid());
		presenter.go(null);

	}

}
